#include "ReflectiveShader.h"


ReflectiveShader::ReflectiveShader(float _amount, vec4 _color) :SurfaceShader(_amount){
	reflectiveColor = _color;
}


ReflectiveShader::~ReflectiveShader(){
}


vec4 ReflectiveShader::getColor(Intersection &_int, Scene* _s){
	//check for max bounce depth
	if (_int.ray()->depth() == _int.ray()->maxDepth()){
		return vec4(0, 0, 0, 0);
	}

	Ray *theRay = _int.ray();
	vec3 pt = _int.getPos();

	//vec3 invDir = theRay->direction() * -1.0f;
	vec3 reflectDir = reflect(theRay->direction(), _int.surfNorm());

	
	//reverse the direction of the ray and reflect it across the normal
	Ray *reflectRay = new Ray(pt, reflectDir, theRay->maxDepth(), theRay->depth() + 1, 2);
	//shoot a ray in the reflected direction
	Intersection _int2 = Intersection(reflectRay);
	
	//get a pointer to the BVH
	BVH* _bvh = _s->theBVH();
	_bvh->intersect(_int2);
	
	/*//THE OLD WAY
	vector<Shape*>::iterator shapes = _s->shapes();	//shapes in the scene
	int numShapes = _s->shapeCount();
	for (int s = 0; s < numShapes; s++){
		Shape *nextShape = *shapes;
		nextShape->intersect(_int2);
		shapes++;
	}
	//END THE OLD WAY*/

	//if it didn't intersect anything
	if (!_int2.intersected()){
		return vec4(0, 0, 0, 0);
	}
	//return the reflected color
	else{
		vec4 returnCol = _int2.shape()->getColor(_int2) * this->amount();
		delete reflectRay;
		return returnCol;
	}
}
